<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>网页聊天程序</title>
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/client.css">
    <link rel="stylesheet" href="css/goodlooking/bootstrap/bootstrap-icons.css">
    <link rel="stylesheet" href="css/goodlooking/bootstrap/min.css">
    <link rel="stylesheet" href="css/goodlooking/tooplate-kool-form-pack.css">

</head>
<body>
<!--    &lt;!&ndash; 作为遮罩层, 实现背景模糊的效果 &ndash;&gt;-->
    <div class="cover"></div>

    <!-- 这个 div 作为整个界面的总的容器 -->
    <div class="client-container">

<!--        header-->
        <div class="col-lg-12 col-12 d-flex align-items-center">
            <a href="javascript:void(0);" class="custom-btn custom-border-btn btn" data-bs-toggle="modal" data-bs-target="#subscribeModal">
                加好友
                <i class="bi-arrow-right ms-2"></i>
            </a>

        </div>


<!--        弹窗  modal.hide();-->
        <div class="modal fade" id="subscribeModal" tabindex="-1">
            <div class="modal-dialog modal-dialog-centered modal-dialog-scrollable">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>

                    <div class="modal-body">
                        <form action="javascript:void(0);" method="post" class="custom-form mt-lg-4 mt-2" role="form">
                            <h2 class="modal-title" id="subscribeModalLabel">添加朋友</h2>

                            <input type="text" name="email" id="email"  class="form-control" placeholder="her userId" required="">
                            <input type="text" name="email" id="content"  class="form-control" placeholder="打个招呼呗" required="">

                            <button type="submit" id = "submit" class="form-control">append</button>
                        </form>
                    </div>

                    <div class="modal-footer justify-content-center">
                        <p>Aba Aba, inarticulate in ideation's void</p>
                    </div>
                </div>
            </div>
        </div>

        <!-- 中间的聊天区域 -->
        <div class="main">
            <!-- 左侧区域, 显示好友列表和会话列表 -->
            <div class="left">
                <!-- 当前登录用户的用户名 -->
                <div class="user">
                </div>
                <!-- 这是搜索框 -->
                <div class="search">
                    <!-- 搜索框中的输入框 -->
                    <input type="text" class="search-input">
                    <!-- 点击按钮进行搜索 -->
                    <button class="search-button"></button>
                </div>
                <!-- 标签页 -->
                <div class="tab">
                    <!-- 会话列表标签 -->
                    <div class="tab-session">
                    </div>
                    <!-- 好友列表标签 -->
                    <div class="tab-friend"></div>
                </div>
                <!-- 会话列表 -->
                <ul class="list" id="session-list">

                </ul>
                <!-- 好友列表 -->
                <ul class="list hide" id="friend-list">

                </ul>

            </div>
            <!-- 右侧区域, 显示聊天编辑框 -->
            <div class="right">
                <!-- 会话标题 -->
                <div class="title"></div>
                <!-- 消息区 -->
                <div class="message-show" id="message-show">
<!--                    <div class="box" data-id="128"><h4>xxx</h4><p>nihao</p></div>-->
                </div>

                <!-- 消息编辑框 -->
                <textarea class="message-input" id="message-input"></textarea>

                <!-- 发送按钮 -->
                <div class="ctrl">
                    <button>发送</button>
                </div>
            </div>
        </div>
    </div>

    <!-- 引入 jquery, 注意, 这个引入操作要在使用之前! 否则会找不到相关函数的定义 -->
    <script src="js/jquery.min.js"></script>
    <script src="../static/js/client.js"></script>
    <script src="js/bootstrap.bundle.min.js"></script>

<script>

    //这里实现enter直接发送的功能，并且shift+enter是换行 用class居然会报错
    const messageInput = document.getElementById('message-input');

    messageInput.addEventListener('keydown', (event) => {
        if (event.key === 'Enter') {
            if (event.shiftKey) return;
            event.preventDefault();
            $(".ctrl").click();
        }
    });

    document.getElementById('friend-list').addEventListener('click', function (event) {
        const target = event.target;

        // 判断点击的是同意按钮
        if (target.classList.contains('agree')) {
            const listItem = target.closest('.friend-request');
            const friendId = listItem.getAttribute('friend-id');

            agreeRequest(friendId);

            listItem.remove();

            getFriendList();

            friendSessionList();

            console.log('同意好友请求，ID:', friendId);

        }

        if (target.classList.contains('refuse')) {
            const listItem = target.closest('.friend-request');
            const friendId = listItem.getAttribute('friend-id');

            // 执行你的业务逻辑
            listItem.remove();
            console.log('拒绝好友请求，ID:', friendId);
        }
    });

    $(".tab .tab-session").click();

    // enter直接搜索
    const searchB = document.querySelector('.search-input');

    searchB.addEventListener('keydown', (event) => {
        if (event.key === 'Enter') {
            event.preventDefault();
            $(".left .search button").click();
        }
    });

    //搜索行为
    $(".left .search button").click(search);

    function search () {
        let sear = $(".left .search input").val();
        console.log("查询");
        $(".left #session-list li h3").each(function () {
            let name = this.textContent;
            if (name == sear) {
                this.click();
            }
        });
    }

    // 监听父元素的右键事件 删除用户
    document.getElementById('friend-list').addEventListener('contextmenu', function (event) {
        const box = event.target.closest('.box-friend');
        if (!box) return;
        event.preventDefault();

        //  会话的id
        const id = box.getAttribute("friend-id");

        // 移除旧菜单
        const existingMenu = document.querySelector('.custom-menu');
        if (existingMenu) existingMenu.remove();

        // 获取当前被点击的 box 元素的位置信息
        const boxRect = box.getBoundingClientRect();

        // 创建菜单并定位到 box 的底部
        const menu = document.createElement('div');
        menu.className = 'custom-menu';

        // 定位逻辑：菜单显示在 box 的右下方
        menu.style.left = `${boxRect.right - 120}px`; // 120 是菜单宽度（需与 CSS 一致）
        menu.style.top = `${boxRect.bottom + 5}px`;   // 5px 间距

        showCustomMenu(event,id,"delFriend"); // 调用显示菜单的方法

    });

    // 监听父元素的右键事件 删除消息
    document.getElementById('message-show').addEventListener('contextmenu', function (event) {
        const box = event.target.closest('.box');
        if (!box) return;
        event.preventDefault();

        const id = box.dataset.id;

        // 移除旧菜单
        const existingMenu = document.querySelector('.custom-menu');
        if (existingMenu) existingMenu.remove();

        // 获取当前被点击的 box 元素的位置信息
        const boxRect = box.getBoundingClientRect();

        // 创建菜单并定位到 box 的底部
        const menu = document.createElement('div');
        menu.className = 'custom-menu';

        // 定位逻辑：菜单显示在 box 的右下方
        menu.style.left = `${boxRect.right - 120}px`; // 120 是菜单宽度（需与 CSS 一致）
        menu.style.top = `${boxRect.bottom + 5}px`;   // 5px 间距

        showCustomMenu(event,id,"delMsg"); // 调用显示菜单的方法
    });

</script>
</body>
</html>